package com.example.hw_smart_home;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.example.hw_smart_home.huawei_api.auth.Authentication;
import com.example.hw_smart_home.huawei_api.util.Constants;
import com.example.hw_smart_home.huawei_api.util.UnsafeOkHttpClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

@RunWith(AndroidJUnit4.class)
public class IssueCommandTest {
    @Test
    public void test() throws Exception {
        String commandName = "Waring";
        String serviceId = "esp8266";
        Map<String, Object> paras = new HashMap<>();
        paras.put("Value", BigDecimal.valueOf(34.1));

        String result = issueCommandByToken(Constants.PROJECT_ID_LT, Constants.DEVICE_ID_LT,
                commandName,
                serviceId,
                paras);

        System.out.println(result);
    }

    public static String issueCommandByToken(String projectId, String deviceId, String commandName, String serviceId, Map<String, Object> parasMap) throws Exception {
        String token = Authentication.getToken(); // 获取 IAM Token
        String url = String.format(Constants.Issue_COMMAND_URL, projectId, deviceId);

        // 请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Auth-Token", token);

        // 构造json请求体
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode body = mapper.createObjectNode();
        body.put("command_name", commandName);
        body.put("service_id", serviceId);

        ObjectNode parasNode = mapper.createObjectNode();
        for (Map.Entry<String, Object> entry : parasMap.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof Number) {
                parasNode.put(entry.getKey(), ((Number) value).doubleValue());
            } else if (value instanceof Boolean) {
                parasNode.put(entry.getKey(), (Boolean) value);
            } else {
                parasNode.put(entry.getKey(), value.toString());
            }
        }

        body.set("paras", parasNode);
        String jsonBody = body.toString();

        // 发送 POST 请求
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(99, TimeUnit.SECONDS) // 设置连接超时
                .readTimeout(99, TimeUnit.SECONDS)    // 设置读取超时
//                .writeTimeout(99, TimeUnit.SECONDS)   // 设置写入超时
                .build();

        RequestBody requestBody = RequestBody.create(jsonBody, MediaType.parse("application/json"));
        Request.Builder builder = new Request.Builder()
                .url(url)
                .post(requestBody);
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            builder.addHeader(entry.getKey(), entry.getValue());
        }

        try (Response response = client.newCall(builder.build()).execute()) {
            if (response.body() != null) {
                return response.body().string();
            } else {
                return "No response body";
            }
        }
    }
}